package com.zxt.dental_sys.Controller.Anxiety;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxt.dental_sys.model.AnxietyInfo;
import com.zxt.dental_sys.model.vo.RestBean;
import com.zxt.dental_sys.service.impl.AnxietyInfoServiceImpl;
import com.zxt.dental_sys.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/anxiety")
public class AnxietyController {

    @Resource
    AnxietyInfoServiceImpl anxietyInfoService;

    @Resource
    JwtUtil jwtUtil;

    @RequestMapping("/add")
    public RestBean addAnxiety(@RequestBody AnxietyInfo anxietyInfo, HttpServletRequest request){
        String uuid = jwtUtil.getJwtUUID(request);
        try {
            if (anxietyInfo.getPatientId() == null) {
                anxietyInfo.setPatientId(uuid);
            }
        }catch (Exception e){
            anxietyInfo.setPatientId(uuid);
        }
        if(anxietyInfo.getScores() < 11){
            anxietyInfo.setAneshesiaPlan("局部麻醉");
        } else if (anxietyInfo.getScores() < 19) {
            anxietyInfo.setAneshesiaPlan("镇静麻醉");
        }else{
            anxietyInfo.setAneshesiaPlan("静脉麻醉");
        }
        anxietyInfo.setAnxietyId(UUID.randomUUID().toString());
        anxietyInfo.setInputTime(new Date());
        return anxietyInfoService.save(anxietyInfo)?
                RestBean.success(anxietyInfo.getAneshesiaPlan()):RestBean.failure(500,"server error");
    }

    @RequestMapping("/getMyAnxiety")
    public RestBean getMyAllAnxiety(HttpServletRequest request){
        String uuid = jwtUtil.getJwtUUID(request);
        return RestBean.success(getAnxietyByUUID(uuid));
    }

    public List<AnxietyInfo> getAnxietyByUUID(String  uuid){
        return anxietyInfoService.list(new QueryWrapper<AnxietyInfo>()
                .eq("patient_id",uuid)
                .eq("is_deleted",0)
        );
    }

}
